package com.android.customized;

import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dxyxx.R;
import com.dxyxx.pojo.CommentReply;
import com.nostra13.universalimageloader.core.ImageLoader;

public class ExpandView extends FrameLayout {

	private Animation mExpandAnimation;
	private Animation mCollapseAnimation;
	private boolean mIsExpand;

	public ExpandView(Context context) {
		this(context, null);
		// TODO Auto-generated constructor stub
	}

	public ExpandView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		// TODO Auto-generated constructor stub
	}

	public ExpandView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		initExpandView();
	}

	private void initExpandView() {
		//LayoutInflater.from(getContext()).inflate(R.layout.layout_expand, this, true);

		mExpandAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.expand);
		mExpandAnimation.setAnimationListener(new Animation.AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				setVisibility(View.VISIBLE);
			}
		});

		mCollapseAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.collapse);
		mCollapseAnimation.setAnimationListener(new Animation.AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				setVisibility(View.INVISIBLE);
			}
		});

	}

	public void collapse() {
		if (mIsExpand) {
			mIsExpand = false;
			removeAllViews();// 节省空间
			clearAnimation();
			startAnimation(mCollapseAnimation);
		}
	}

	public void expand() {
		if (!mIsExpand) {
			mIsExpand = true;
			clearAnimation();
			startAnimation(mExpandAnimation);
		}
	}

	public boolean isExpand() {
		return mIsExpand;
	}

	public void setContentView(String baseImageURL, ImageLoader imageLoader, List<CommentReply> list) {
		ViewGroup view = null;
		view = (ViewGroup) LayoutInflater.from(getContext()).inflate(R.layout.layout_expand, null);
		// 
		for (CommentReply reply : list) {
			LinearLayout item = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.item_layout_expand, null);
			((TextView) item.findViewById(R.id.name)).setText(reply.getUserName());
			((TextView) item.findViewById(R.id.word)).setText(reply.getWord());
			imageLoader.displayImage(baseImageURL + reply.getUser_image() + "?p=0", (ImageView) item.findViewById(R.id.image), null, null);

			view.addView(item);
		}

		removeAllViews();
		addView(view);
	}

}